<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 1/22/15
 * Time: 11:10 PM
 */

class Account extends MY_Controller {
    public function notification() {
        $response = new Response();
        try {
            $notification = $this->findById("Notification", 1);
            $response->setData($notification);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function saveNotification(){
        $response = new Response();
        try {
            $notification = $this->findById("Notification", 1);
            $notification->setAgentRegistrationEmail($this->input->post("agentRegistrationNotification"));
            $notification->setOrderPlaceEmail($this->input->post("orderNotification"));
            $notification->setInventoryReportEmail($this->input->post("inventoryNotification"));
            $notification->setOrderMessage($this->input->post("orderMessage"));
            $this->save($notification);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function accountDetails(){
        $response = new Response();
        try {
            $user = $this->getLoggedInUser();
            $user->setPassword("");
            $response->setData($user);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function saveAccountDetails(){
        $response = new Response();
        try {
            $user = $this->getLoggedInUser();
            $user->setEmail($this->input->post("s-email"));
            $user->setUsername($this->input->post("s-username"));
            $this->save($user);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function updatePassword(){
        $response = new Response();
        try {
            $user = $this->getLoggedInUser();
            if($user->getPassword() == $this->input->post("s-currentPassword")){
                if($this->input->post("s-rePassword") == $this->input->post("s-newPassword")){
                    $user->setPassword($this->input->post("s-newPassword"));
                    $this->save($user);
                }else{
                    $response->setError(true);
                    $response->setErrorMessage("New password and Retype password do not match. \n Please try again");
                }
            }else{
                $response->setError(true);
                $response->setErrorMessage("Please enter valid current password.");
            }
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function getSystemConfig(){
        $response = new Response();
        try {
            $systemConfigs = $this->findAll("Systemconfig");
            $response->setData($systemConfigs);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function saveSystemConfig(){
        $response = new Response();
        try {
            $orderCancelDuration= $this->input->post("orderCancelDuration");
            $orderCancelEnable=$this->input->post("orderCancelEnable");

            $durationSystemConfig = parent::getSystemConfig(Systemconfig::CRON_ORDER_CANCEL_HOUR);
            $durationSystemConfig->setValue($orderCancelDuration);
            $this->save($durationSystemConfig);

            $orderCancelEnableSystemConfig = parent::getSystemConfig(Systemconfig::CRON_ORDER_CANCEL_ENABLE);
            if($orderCancelEnable == "true"){
                $orderCancelEnableSystemConfig->setValue("true");
            }else{
                $orderCancelEnableSystemConfig->setValue("false");
            }
            $this->save($orderCancelEnableSystemConfig);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
}
